﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Wicresoft.RDW.Presentation;
using Resources;
using Wicresoft.RDW.Utility;

namespace Wicresoft.RDW.Feedback
{
    public partial class FeedbackSurveyManagement : BasePage_BackEnd
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            pager.PageChange += new Wicresoft.RDW.UserControls.PagerControl.PageRefresh(PagerControll_PageChange);
        }
        public override void InitControlResource()
        {
            int i = 0;
            gvFeedbackSurvey.Columns[i++].HeaderText = NameResource.No;
            gvFeedbackSurvey.Columns[i++].HeaderText = NameResource.Request_RequestUserID;
            gvFeedbackSurvey.Columns[i++].HeaderText = NameResource.Visit_CompanyName;
            gvFeedbackSurvey.Columns[i++].HeaderText = NameResource.Request_DataTime;
            gvFeedbackSurvey.Columns[i++].HeaderText = NameResource.IsPublished;
            gvFeedbackSurvey.Columns[i++].HeaderText = NameResource.Btn_View;

            pager.CurrentPageIndex = 0;
            PagerControll_PageChange(pager.CurrentPageIndex);
        }
        protected void PagerControll_PageChange(int pageIndex)
        {
            string language = Language.ToString();
            var dataPagerSource = FeedbackPresent.GetPaged(pageIndex, pager.PageSize, p => p.Status == (int)FeedbackStatus.Completed, p => p.Request.RequestDate, true);
            gvFeedbackSurvey.DataSource = dataPagerSource;
            gvFeedbackSurvey.DataBind();
            pager.IntialProperties(dataPagerSource.TotalCount);
        }

        protected void gvFeedbackSurvey_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Model.Feedback feedback = e.Row.DataItem as Model.Feedback;
                if (feedback != null)
                {
                    e.Row.Cells[0].Text = (e.Row.RowIndex + 1 + pager.PageSize * pager.CurrentPageIndex).ToString();
                    var request = RequestPresent.GetById(feedback.RequestId);
                    if (request != null)
                    {
                        var visitRequest = VisitRequestPresent.GetByCondition(p => p.RequestId == feedback.RequestId);
                        var user = UserPresent.GetById(request.RequestUserId);
                        if (user != null)
                        {
                            (e.Row.Cells[1].FindControl("lblApplyName") as Label).Text = Language.ToString() == "EN" ? user.NameEN : user.NameCN;
                        }
                        if (visitRequest != null)
                        {
                            (e.Row.Cells[2].FindControl("lblFeedbackName") as Label).Text = visitRequest.CompanyName;
                        }
                        (e.Row.Cells[3].FindControl("lblCreatTime") as Label).Text = string.Format("{0:yyyy-MM-dd}", request.RequestDate);

                    }
                    (e.Row.Cells[4].FindControl("chkIsPublished") as CheckBox).Checked = feedback.IsPublished;
                    if (!IsAdmin)
                    {
                        (e.Row.Cells[4].FindControl("chkIsPublished") as CheckBox).Enabled=false;
                    }
                    (e.Row.Cells[5].FindControl("lnkbtnViewResult") as LinkButton).Text = NameResource.lnkbtnViewResult;
                }
            }
        }

        protected void gvFeedbackSurvey_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int i = e.RowIndex;
            int feedbackId = (int)gvFeedbackSurvey.DataKeys[i]["FeedbackId"];
            var feedback = FeedbackPresent.GetById(feedbackId);
            var visitRequest = VisitRequestPresent.GetByCondition(p => p.RequestId == feedback.RequestId);
            Response.Redirect("/Feedback/Send.aspx?id=" + visitRequest.VisitRequestId);
        }

        protected void chkIsPublished_OnCheckedChanged(object sender, EventArgs e)
        {
            if (IsAdmin)
            {
                CheckBox isPublished = (CheckBox)sender;
                int feedbackId = int.Parse(isPublished.Attributes["Key"]);
                var feedback = FeedbackPresent.GetById(feedbackId);
                feedback.IsPublished = isPublished.Checked;
                FeedbackPresent.Update(feedback);
            }
        }
    }
}
